package org.maxgroundstation.groundstation.comm.service.log;

import javax.swing.SwingUtilities;

import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceCommunication;
import org.maxgroundstation.groundstation.comm.service.ServiceDataFrame;
import org.maxgroundstation.groundstation.comm.service.ServiceUtil;

/**
 * The log service.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class LogService extends Service {

	// Static members
	public static final String SERVICE_ID = ServiceUtil.getServiceID("log");
	public static final String TYPE = "log";
	
	// Session event type.
	public static final String LOG_MESSAGE = "log message";
	
	// Data members.
	private String lastLogMessage;
	
	/**
	 * Constructor
	 * @param comm the service communication.
	 */
	public LogService(ServiceCommunication comm) {
		// Use Service constructor.
		super(comm);
	}
	
	@Override
	public String getID() {
		return SERVICE_ID;
	}
	
	@Override
	public boolean isSubscribable() {
		return true;
	}
	
	@Override
	public void initialize() {
		// Do nothing.
	}
	
	@Override
	public void handleResponse(ServiceDataFrame dataFrame) {
		
		lastLogMessage = dataFrame.getDataString();
		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
			    fireServiceEvent(LOG_MESSAGE, lastLogMessage );
			}
		});
	}

	@Override
	public boolean isDefaultService() {
		return false;
	}

    @Override
    public String getType() {
        return TYPE;
    }
}